import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

// void main() => runApp(const NotificationGroupApp());
//
// class NotificationGroupApp extends StatelessWidget {
//   const NotificationGroupApp({super.key});
//
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       title: '通知群示例',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//         appBarTheme: const AppBarTheme(
//           elevation: 0,
//           backgroundColor: Colors.white,
//           foregroundColor: Colors.black,
//         ),
//       ),
//       home: const NotificationGroupScreen(),
//     );
//   }
// }

class NotificationGroupScreen extends StatefulWidget {
  const NotificationGroupScreen({super.key});

  @override
  State<NotificationGroupScreen> createState() => _NotificationGroupScreenState();
}

class _NotificationGroupScreenState extends State<NotificationGroupScreen> {
  final List<GroupNotification> _notifications = [
    GroupNotification(
      content: '【会议提醒】下午3点302会议室召开项目评审会',
      type: NotificationType.meeting,
      time: DateTime.now().subtract(const Duration(minutes: 5)),
      sender: '行政部',
    ),
    GroupNotification(
      content: '【紧急通知】请各部门立即提交季度报告',
      type: NotificationType.urgent,
      time: DateTime.now().subtract(const Duration(minutes: 30)),
      sender: '总经办',
    ),
    GroupNotification(
      content: '【系统公告】服务器将于今晚23:00进行维护升级',
      type: NotificationType.system,
      time: DateTime.now().subtract(const Duration(hours: 2)),
      sender: '系统管理员',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公司通知群（268人）'),
        actions: [
          IconButton(
            icon: const Icon(Icons.info_outline),
            onPressed: _showGroupInfo,
          ),
        ],
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(16),
        itemCount: _notifications.length,
        itemBuilder: (context, index) {
          final notification = _notifications[index];
          return _NotificationBubble(notification: notification);
        },
      ),
    );
  }

  void _showGroupInfo() {
    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '群信息',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            _buildInfoItem('群名称', '公司通知群'),
            _buildInfoItem('群成员', '268人'),
            _buildInfoItem('创建时间', '2023-01-01'),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoItem(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        children: [
          Text('$title：', style: const TextStyle(color: Colors.grey)),
          const SizedBox(width: 8),
          Text(value),
        ],
      ),
    );
  }
}

class _NotificationBubble extends StatelessWidget {
  final GroupNotification notification;

  const _NotificationBubble({required this.notification});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildTimeStamp(),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildSenderAvatar(),
              const SizedBox(width: 12),
              Expanded(child: _buildContentCard()),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildTimeStamp() {
    return Padding(
      padding: const EdgeInsets.only(left: 48, bottom: 8),
      child: Text(
        _formatTime(notification.time),
        style: TextStyle(
          color: Colors.grey[600],
          fontSize: 12,
        ),
      ),
    );
  }

  String _formatTime(DateTime time) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final yesterday = today.subtract(const Duration(days: 1));

    if (time.isAfter(today)) {
      return '今天 ${DateFormat('HH:mm').format(time)}';
    } else if (time.isAfter(yesterday)) {
      return '昨天 ${DateFormat('HH:mm').format(time)}';
    }
    return DateFormat('yyyy-MM-dd HH:mm').format(time);
  }

  Widget _buildSenderAvatar() {
    IconData icon;
    Color color;

    switch (notification.type) {
      case NotificationType.system:
        icon = Icons.security;
        color = Colors.blue;
        break;
      case NotificationType.urgent:
        icon = Icons.warning_amber_rounded;
        color = Colors.red;
        break;
      case NotificationType.meeting:
        icon = Icons.event_available;
        color = Colors.green;
        break;
    }

    return CircleAvatar(
      radius: 20,
      backgroundColor: color.withOpacity(0.1),
      child: Icon(icon, size: 20, color: color),
    );
  }

  Widget _buildContentCard() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 2,
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(
                  notification.sender,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 14,
                  ),
                ),
                const SizedBox(width: 8),
                _buildTypeLabel(),
              ],
            ),
            const SizedBox(height: 8),
            Text(
              notification.content,
              style: const TextStyle(fontSize: 15),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTypeLabel() {
    String text;
    Color color;

    switch (notification.type) {
      case NotificationType.system:
        text = '系统通知';
        color = Colors.blue;
        break;
      case NotificationType.urgent:
        text = '紧急通知';
        color = Colors.red;
        break;
      case NotificationType.meeting:
        text = '会议通知';
        color = Colors.green;
        break;
    }

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: color,
          fontSize: 12,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}

// 数据模型
class GroupNotification {
  final String content;
  final NotificationType type;
  final DateTime time;
  final String sender;

  GroupNotification({
    required this.content,
    required this.type,
    required this.time,
    required this.sender,
  });
}

enum NotificationType { system, urgent, meeting }
